package com.youlai.boot.word.service.impl;

import com.youlai.boot.common.constant.MongoConstants;
import com.youlai.boot.mongo.MongoCommonService;
import com.youlai.boot.word.entity.QueryForm;
import com.youlai.boot.word.entity.WordEntity;
import com.youlai.boot.word.entity.WordListEntity;
import com.youlai.boot.word.service.WordListMongoInterface;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class WordListMongoService implements WordListMongoInterface {

    @Autowired
    private MongoCommonService mongoCommonService;

    /**
     * 全部
     * 单词列表
     */
    @Override
    public List<?> getAllListData(String collectionName) {
        Query query = new Query();
        return mongoCommonService.getList(new WordEntity(), query, collectionName);
    }

    /**
     * 全部
     * 分页查询
     * 单词列表
     */
    @Override
    public List<?> getAllListDataByPage(String collectionName, long num, long page) {
        Query query = new Query();
        query.skip((page - 1) * num);
        return mongoCommonService.getList(new WordEntity(), query, collectionName);
    }

    @Override
    public List<?> getAllListDataByPage(String collectionName, QueryForm queryForm) {
        int pageSize = queryForm.getPageSize();
        int pageNum = queryForm.getPageNum();
        return getAllListDataByPage(collectionName, pageNum, pageSize);
    }

    /**
     * 已掌握单词列表
     */
    @Override
    public List<?> getListData(String collectionName) {
        return getListData(collectionName, MongoConstants.WORK_READ_TYPE_KNOWN);
    }

    /**
     * 按类型查询
     * 单词列表
     */
    @Override
    public List<?> getListData(String collectionName, String type) {
        Query query = new Query();
        Criteria criteria = Criteria.where(MongoConstants.MONGO_TYPE).is(type);
        query.addCriteria(criteria);
        return mongoCommonService.getList(new WordListEntity(), query, collectionName);
    }

    @Override
    public void createCollection(String collectionName) {
        mongoCommonService.createCollection(collectionName);
    }

    @Override
    public void insertOne(WordListEntity entity) {
        mongoCommonService.insertOne(entity);
    }

    @Override
    public void insertOne(WordListEntity entity, String collectionName) {
        mongoCommonService.insertOne(entity, collectionName);
    }

    @Override
    public void insertList(List<WordListEntity> list) {
        mongoCommonService.insertList(list);
    }

    @Override
    public void insertList(List<WordListEntity> list, String collectionName) {
        mongoCommonService.insertList(list, collectionName);
    }

    /**
     * 修改单词学习状态
     * 全部
     */
    @Override
    public void updateListType(String type) {
        Query query = new Query();
        Update update = new Update();
        update.set(MongoConstants.MONGO_TYPE, type);
        mongoCommonService.update(WordListEntity.class);
    }

    /**
     * 修改单词学习状态
     * 单个
     */
    @Override
    public void updateOneType(String type) {
        Query query = new Query();
        Update update = new Update();
        update.set(MongoConstants.MONGO_TYPE, type);
        mongoCommonService.updateOne(WordListEntity.class, update, query);
    }
}
